import QtQuick 1.0

Item {
    id: game
    property int blah: 1
    property string lastGuess
    property string notWordTag: "notword"
    property string giveUpTag: "giveup"
    property string givenUpTag: "givenup"
    property bool won: false

    height:640
    function submitGuess(text) {
        var wordMaster = baseScreen.wordMaster();
        if (wordMaster.isWord(text)) {
            var guess = wordMaster.guess(text)
            if (guess.results.length === 4) {
                var win = true;
                for (var i=0; i<guess.results.length; i++) {
                    if (!guess.results[i]) {
                        win = false;
                        break;
                    }
                }

                if (win) {
                    // You're a winner!
                    won = true
                    anim.disappear();
                }
            }

            wordEntry.clear()
            game.blah++
            repositionFlickable()
        } else {
            lastGuess = text
            balloon.show(notWordTag, game.lastGuess + " is not a word!  All guesses must be valid English words.")
        }
    }

    ListModel {
        id: gameModel
    }

    PlaneHeader {
        id: anim
        text: "WORD MASTER"
        animType: anim.animNone
        height: 120
        z:2

        onDisappearDone: {
            anim.text = baseScreen.wordMaster().target().toUpperCase();
            anim.reappear();
        }
    }

    Balloon {
        id: balloon
        z: 3

        onDismissed: {
            if (tag == givenUpTag) {
                loader.source = "Menu.qml";
            }
        }

        onYes: {
            if (tag == giveUpTag) {
                balloon.show(givenUpTag, "The word was: " + baseScreen.wordMaster().target())
            }
        }

        onNo: {
            if (tag == giveUpTag) {
                balloon.hide();
            }
        }
    }

    Item {
        y: 120
        width: parent.width
        height: parent.height - 120

        Flickable {
            anchors.fill: parent
            height: parent.height
            anchors.margins: 10
            id: flickable
            contentHeight: wmCol.height
            Column {
                id: wmCol
                width: parent.width
                height: childrenRect.height

                Repeater {
                    model: baseScreen.wordMaster().numGuesses() * (blah/blah)
                    delegate: WordmasterRow { }
                }

                CurrentWordEntry {
                    id: wordEntry
                    visible: !won
                    count: baseScreen.wordMaster().numGuesses() * (blah/blah)
                }
            }

            onContentHeightChanged: {
                repositionFlickable()
            }
        }
    }

    Image {
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        anchors.margins: 10
        source: "assets/cross.png"

        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (!won) {
                    balloon.confirm(giveUpTag, "Really give up?")
                } else {
                    loader.source = "Menu.qml";
                }
            }
        }
    }

    function repositionFlickable() {
        // Scroll the flickable all the way to the bottom
        if (flickable.contentHeight > flickable.height) {
            flickable.contentY = -(flickable.height - flickable.contentHeight)
        }
    }
}
